<?php

namespace Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Builder;

class Base extends Model
{
    use HasFactory;

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 全局搜索
     * 
     * 在需要增加全局搜索的模型 booted() 中调用
     * 支持前端自定义搜索以及排序(orderby:排序字段, sort:asc/desc)
     *
     * @return void
     */
    protected static function searchs()
    {
        // 全局作用域
        // searchs = {"field": "=|value", ...}
        //{'title' : 'like|%xxxx%', 'desc': '=|xxxxx', 'price': '>|5'}
        static::addGlobalScope('searchs', function (Builder $builder) {
            $builder->when((request()->filled('searchs')), function ($query) {
                $search = request()->searchs;
                if (is_string($search)) {
                    $search = json_decode($search, true, 512, JSON_BIGINT_AS_STRING);
                }
                foreach ($search as $key => $value) {
                    // 禁止 uid 直接搜索
                    if (in_array($key, ['uid'])) continue;
                    $value = explode('|', $value);
                    if ($value) {
                        switch ($value[0]) {
                            case 'in':
                                $query->whereIn($key, explode(',', $value[1]));
                                break;
                            case 'notin':
                                $query->whereNotIn($key, explode(',', $value[1]));
                                break;
                            case 'null':
                                $query->whereNull($key);
                                break;
                            case 'notnull':
                                $query->whereNotNull($key);
                                break;
                            case 'between':
                                $query->whereBetween($key, explode(',', $value[1]));
                                break;
                            case 'like':
                                $query->where($key, 'like', '%' . $value[1] . '%');
                                break;
                            case 'left_like':
                                $query->where($key, 'like', $value[1] . '%');
                                break;
                            case 'right_like':
                                $query->where($key, 'like', '%' . $value[1]);
                                break;
                            default:
                                $query->where($key, $value[0], $value[1]);
                                break;
                        }
                    }
                }
            });
        });
        // 排序
        static::addGlobalScope('sortBy', function (Builder $builder) {
            $builder->when(
                request()->filled('orderby') && request()->filled('sort'),
                function ($query) {
                    $query->orderBy(request()->orderby, str_ireplace('ending', '', request()->sort));
                }
            );
        });
    }
}
